package com.cetccloud.ap.proxy.processor.model;

import com.cetccloud.ap.util.ResultEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName ProcessReturn
 * @Description TODO
 * @Author superz
 * @Date 2021/4/21 11:20
 * @Version 1.0
 **/
@Data
public class ProcessResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回码(200:成功; 非200:失败)
     **/
    private Integer code;

    /**
     * 成功返回“成功”，失败返回对应的失败信息
     **/
    private String msg;

    /**
     * 响应体
     **/
    private T body;

    public static ProcessResult success() {
        ProcessResult processResult = new ProcessResult();
        processResult.setCode(ResultEnum.SUCCESS.getCode());
        processResult.setMsg(ResultEnum.SUCCESS.getMsg());
        return processResult;
    }

    public static ProcessResult error() {
        ProcessResult processResult = new ProcessResult();
        processResult.setCode(ResultEnum.ERROR.getCode());
        processResult.setMsg(ResultEnum.ERROR.getMsg());
        return processResult;
    }

    public static ProcessResult error(String msg) {
        ProcessResult processResult = new ProcessResult();
        processResult.setCode(ResultEnum.ERROR.getCode());
        processResult.setMsg(msg);
        return processResult;
    }

    public ProcessResult<T> build(T result) {
        this.body = result;
        return this;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return ResultEnum.SUCCESS.getCode().equals(this.getCode());
    }
}
